package cn.ibobo.lease.app.service.impl;


import cn.ibobo.lease.app.dao.LeaseResDetailMapper;
import cn.ibobo.lease.app.dao.ResLeaseMapper;
import cn.ibobo.lease.app.dao.UserDepositMapper;
import cn.ibobo.lease.app.dao.UserMapper;
import cn.ibobo.lease.app.service.ResLeaseService;
import cn.ibobo.lease.common.base.BaseMapper;
import cn.ibobo.lease.common.base.BaseServiceImpl;
import cn.ibobo.lease.common.dto.LeaseResDetailDTO;
import cn.ibobo.lease.common.dto.ResLeaseDTO;
import cn.ibobo.lease.common.dto.UserDTO;
import cn.ibobo.lease.common.dto.UserDepositDTO;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.DateUtil;
import cn.ibobo.lease.common.util.UuidUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;


/**
 * 物品租赁表业务处理接口
 * @Author lililiang
 * @Date 2018-12-13 18:36:40
 */
@Service
public class ResLeaseServiceImpl extends BaseServiceImpl<ResLeaseDTO, String> implements ResLeaseService {

    private static final Log LOGGER = LogFactory.getLog(ResLeaseServiceImpl.class);

    @Autowired
    private ResLeaseMapper mapper;
    @Autowired
    private LeaseResDetailMapper leaseResDetailMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    protected BaseMapper<ResLeaseDTO, String> getMapper() {
        return mapper;
    }

    /**
     * 物品租赁表列表
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public List<ResLeaseDTO> list4Page(ResLeaseDTO entity, Integer pageNum, Integer... pageSize) {
        return super.list4Page(entity, pageNum, pageSize);
    }

    /**
     * 物品租赁表列表（无分页）
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public List<ResLeaseDTO> listByCondition(ResLeaseDTO entity) {
        return super.listByCondition(entity);
    }

    /**
     * 查询物品租赁表详情信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    public ResLeaseDTO queryOneByPk(String id) {
        return super.queryOneByPk(id);
    }

    /**
     * 保存物品租赁表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer save(ResLeaseDTO entity) {
        String id = entity.getId();
        if(id == null || id.length() == 0){
            entity.setId(UuidUtil.getUuid());
            return mapper.add(entity);
        }else{
            return mapper.edit(entity);
        }
    }

    /**
     * 删除物品租赁表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @Override
    @Transactional
    public Integer delete(String id) {
        return super.delete(id);
    }

    /**
     * @Description 申请物品租赁
     * @Author lililiang
     * @Date 2018/12/23 11:26
     * @Param [entityDTO]
     * @Return java.lang.Integer
     */
    @Override
    @Transactional
    public Integer lease(ResLeaseDTO entityDTO) {
        //1. 校验租赁物品状态
        LeaseResDetailDTO leaseResDetailDTO = leaseResDetailMapper.queryOneByPk(entityDTO.getDid());
        if(leaseResDetailDTO == null ){
            return ResultEnum.LEA_NO_EXIST.getCode();
        }else if(leaseResDetailDTO.getStatus() != 1 || leaseResDetailDTO.getRstatus() != 1){
            return ResultEnum.ERR_LEA_STATUS.getCode();
        }
        //2. 校验押金
        UserDTO userDTO = userMapper.queryOneByPk(entityDTO.getUid());
        if(userDTO == null || userDTO.getDeposit() == null
                || userDTO.getDeposit().compareTo(leaseResDetailDTO.getDeposit()) < 0){
            return ResultEnum.NO_ENOUGH_DEPOSIT.getCode();
        }
        //3. 新增租赁记录
        entityDTO.setId(UuidUtil.getUuid());
        entityDTO.setCreatetm(DateUtil.getNow());
        entityDTO.setDeposit(leaseResDetailDTO.getDeposit());
        entityDTO.setPrice(leaseResDetailDTO.getPrice());
        entityDTO.setLid(leaseResDetailDTO.getLid());
        if(mapper.add(entityDTO) <= 0){
            return ResultEnum.FAIL.getCode();
        }
        //4. 修改租赁物品状态，到店领取后修改租赁物品状态
//        leaseResDetailDTO = new LeaseResDetailDTO();
//        leaseResDetailDTO.setDid(entityDTO.getDid());
//        leaseResDetailDTO.setStatus(2);
//        if(leaseResDetailMapper.edit(leaseResDetailDTO) <= 0){
//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//            return ResultEnum.FAIL.getCode();
//        }
        return ResultEnum.SUCCESS.getCode();
    }

    /**
     * @Description 物品归还
     * @Author lililiang
     * @Date 2018/12/23 11:26
     * @Param [entityDTO]
     * @Return java.lang.Integer
     */
    @Override
    @Transactional
    public Integer back(ResLeaseDTO entityDTO){
        //1. 校验租赁物品信息
        ResLeaseDTO resLeaseDTO = mapper.queryOneByPk(entityDTO.getId());
        if(resLeaseDTO == null){
            return ResultEnum.NO_LEA_REC.getCode();
        }else if(resLeaseDTO.getStatus() != 1){
            return ResultEnum.ERR_USE_STATUS.getCode();
        }
        //2. 计算租赁费用-暂不校验

        //3. 修改租赁记录状态
        entityDTO.setBacktm(DateUtil.getNow());
        entityDTO.setStatus(2);
        if(mapper.edit(entityDTO) <= 0){
            return ResultEnum.FAIL.getCode();
        }
        //4. 修改租赁物品库存状态
        LeaseResDetailDTO leaseResDetailDTO = new LeaseResDetailDTO();
        leaseResDetailDTO.setDid(resLeaseDTO.getDid());
        leaseResDetailDTO.setStatus(1);
        if(leaseResDetailMapper.edit(leaseResDetailDTO) <= 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultEnum.FAIL.getCode();
        }
        return ResultEnum.SUCCESS.getCode();
    }
}
